/*
 * 作者: 袁燚杰
 * 时间: 2017-03-06
 * 说明：
 * Copyright (c) 2016-2017 中研沃兮
 */
define(function(require) {

	var c = require('app/utils/CommonUtils');
	var UserInfoUtils = require('app/utils/UserInfoUtils');
	var EventUtils = require('app/utils/EventUtils');
	var hc = require('app/utils/HttpClientUtils');
	var appInfo = require('app/AppInfo');
	var APIUtils = require('app/utils/APIUtils');
	var aliyunLive = api.require('aliyunLive');
	var tip = require('app/utils/TipUtils');
	var isStarted = false;
	var action = {};
	var param = api.pageParam;
	var playurl = param.pages[0].playurl;
	var publishurl = param.pages[0].publishurl;
	var publishid = param.pages[0].publishid;
	
	var isIos = (api.systemType == 'ios');

	if(isIos){
		var winW = APIUtils.winW();
		var winH = APIUtils.winH();
	}else{
		var winW = APIUtils.winH();
		var winH = APIUtils.winW();
	}
		
	action.closeWindow = function() {
		
		api.closeFrame({
			name : "LiveMaskIOSFrm"
		});
		
		api.closeFrame({
			name : "LiveMaskAndroidFrm"
		});
		var roomuuid = param.uuid;
		
		var url = appInfo.serverbase + "/wjt/app/close_publish";
		
		var body = {
			roomuuid: roomuuid
		};
		
		
		hc.post(url,body,function(resp){
			
			api.closeWin({
		
			});
			api.setScreenOrientation({
    			orientation: 'portrait_up'
			});
			EventUtils.send("RoomWin","close_stream_publish");
		});
	

	}
	
	action.initStream = function(){
		
		//横屏
		aliyunLive.configStream({
			rect : {
				x : 0,
				y : 0,
				w : winW,
				h : winH
			},
			url : publishurl,//'rtmp://pili-publish.haojs.com.cn/haojslive/58c2b187a3d5ec0fac7231d9?key=f6e80215e1a59c0d',
			bitRate : {
				videoMaxBitRate : 720 * 1000,
				videoMinBitRate : 240 * 1000,
				videoBitRate : 300 * 1000,
				audioBitRate : 64 * 1000
			},
			fps : 30,
			screenOrientation : 'horizontal',
			reconnectTimeout : 5,
			videoResolution : '540P',
			videoPreset : '1280*720',
			cameraPosition : 'back',
			waterMarkImage : {
				path : '',
				location : 'leftTop',
				maginX : 20,
				maginY : 20
			},
			fixed : true
		}, function(ret) {
			if (ret.status == true) {
				
				action.openLiveMaskFrm();
				action.addConnectStatusListener();
				action.addEventListener();
	
			} else {
				tip.show("预览失败，请重试！");
			}
	
		});
		
	}
	
	action.openLiveMaskFrm = function() {
		if(isIos){
			var name = "LiveMaskIOSFrm";
		}else{
			var name = "LiveMaskAndroidFrm";
		}
		
		api.openFrame({
			name : name,
			url : './' + name + '.html',
			rect : {
				x : 0,
				y : 0,
				w : winW,
				h : winH
			},
			bgColor : 'rgba(0,0,0,0)'
		});
	}
	
	action.addConnectStatusListener = function() {
		aliyunLive.addConnectStatusListener(function(ret) {
			// 0 代表未知状态
			// 1 代表开始连接
			// 2 代表连接成功
			// 3 代表连接失败
			// 4 代表连接销毁
			// 5 代表网络繁忙
			var data = {};
			data.code = ret.code
			EventUtils.send(api.winName, 'ConnectStatus', data);
		});
	}


	action.addEventListener = function() {
		EventUtils.listen(api.winName, 'LiveMask', false, function(data) {
			
			if (data.command == "start") {
			
				if (!isStarted) {
					aliyunLive.startStream(function(ret) {
						if (ret.status == true) {
							isStarted = true;
							var data = {};
							data.status = true;
							EventUtils.send(api.winName, 'VideoUtils', data);
						} else {
							tip.show("推流失败，请重试！");
						}
					});
				} else {
					aliyunLive.stopStream(function(ret) {
						if (ret.status == true) {
							isStarted = false;
							var data = {};
							data.status = false;
							EventUtils.send(api.winName, 'VideoUtils', data);
						} else {
							tip.show("关闭流失败，请重试！");
						}
					});
				}
			}
			if (data.command == "exit") {
				api.confirm({
					title : '提醒',
					msg : '是否退出直播？',
					buttons : ['确定', '取消']
				}, function(ret, err) {
					var index = ret.buttonIndex;
					if (index == 1) {
						if (!isStarted) {
							aliyunLive.destroyStream();
							action.closeWindow();	
						} else {
							
							aliyunLive.stopStream(function(ret) {
							
							c.l(ret);
								if (ret.status) {
									isStarted = false;
									aliyunLive.destroyStream();
									action.closeWindow();
								} else {
									tip.show("关闭流失败，请重试！");
								}
							});
						}
		
					}

				});

			}

		});
	}


	return action;
});
